package org.sn.jdish.interview.dmall.payment.util;

import java.lang.reflect.InvocationTargetException;

import org.sn.jdish.interview.dmall.payment.IpaymentStrategy;

public class PaymentStrategyUtil {

	public static IpaymentStrategy getStrategy(int type) throws ClassNotFoundException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		// 这里最好是配置在数据库中type对应的 策略实现类的路径
		String classPath = String.valueOf(type);
		Class<?> clazz = Class.forName("org.sn.jdish.interview.dmall.payment.impl.AliPaymentStrategy");
		IpaymentStrategy ipaymentStrategy = (IpaymentStrategy) clazz.getDeclaredMethod("getInstance").invoke(null,
				null);
		return ipaymentStrategy;
	}

	public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		int type = 1;
		int userId = 1;
		int fee = 1;
		PaymentStrategyContext context = new PaymentStrategyContext();
		context.setIpaymentStrategy(PaymentStrategyUtil.getStrategy(type));
		context.exePay(userId, fee);
	}

}
